class Solution {
public:
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    void solve(vector<vector<char>>& board)
    {
        int n = board.size(), m = board[0].size();
        vector<vector<bool>> vis(n, vector<bool>(m, false));
        function<void(int, int)> dfs = [&](int i, int j) {
            vis[i][j] = true;
            for (int a = 0; a < 4; a++)
            {
                int x = dx[a] + i, y = dy[a] + j;
                if (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && board[x][y] == 'O')
                    dfs(x, y);
            }
            };

        for (int i = 0; i < n; i++) if (board[i][0] == 'O') dfs(i, 0);
        for (int i = 0; i < n; i++) if (board[i][m - 1] == 'O') dfs(i, m - 1);
        for (int i = 0; i < m; i++) if (board[0][i] == 'O') dfs(0, i);
        for (int i = 0; i < m; i++) if (board[n - 1][i] == 'O') dfs(n - 1, i);

        for (int i = 0; i < n; i++)for (int j = 0; j < m; j++)if (!vis[i][j] && board[i][j] == 'O') board[i][j] = 'X';
    }
};